package com.itgungnir.designpattern.interpreter;

import java.util.HashMap;
import java.util.Map;

/**
 * 上下文类，存储系统中所有常量和变量
 */
public class Context {
	private Map<String, VariableExpression> variableMap;

	public Context() {
		this.variableMap = new HashMap<String, VariableExpression>();
	}

	// 存储数值
	public Expression addVariable(String key, Integer value) {
		Expression expression = null;
		if (!variableMap.containsKey(key)) {
			expression = new VariableExpression(key, value);
			this.variableMap.put(key, (VariableExpression) expression);
		}
		return expression;
	}

	// 取出数值
	public VariableExpression getVariable(String key) {
		if (variableMap.containsKey(key)) {
			return variableMap.get(key);
		}
		return null;
	}
}